package com.acwing.chapter3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author anan
 * @create 2021-08-02 22:33
 */
public class Duilie {
    private static int N = 100010;
    private static int[] queue = new int[N];
    private static int hh;
    private static int tt = -1;

    private static void push(int x){
        queue[++tt] = x;
    }

    private static void pop(){
        hh++;
    }

    private static void empty(){
        if (hh > tt) System.out.println("YES");
        else System.out.println("NO");
    }

    private static int query(){
        return queue[hh];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] str1 = reader.readLine().split(" ");
        int M = Integer.parseInt(str1[0]);
        int x;
        while (M-- > 0){
            String[] str2 = reader.readLine().split(" ");
            switch (str2[0]){
                case "push":
                    x = Integer.parseInt(str2[1]);
                    push(x);
                    break;
                case "pop":
                    pop();
                    break;
                case "empty":
                    empty();
                    break;
                case "query":
                    System.out.println(query());
                    break;
            }
        }
    }
}
